﻿
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Text;
using System.IO;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;

namespace Syrinx
{
	public class TempBizObjListBuilderBizRules : BaseBizRules
	{
		public IBizObjList getTempBizObjList(IApplication app, IRuleContext context, string id)
		{
			IBizObjList tempList = app.sessionData.get("tempList"+id) as IBizObjList;
			if (tempList == null)
				tempList = app.bizObjMgr.createList("BizObj");
			return tempList;
		}

		public void addBizObjToTempBizObjList(IApplication app, IRuleContext context, string id, IBizObj item)
		{
			IBizObjList tempList = getTempBizObjList(app, context, id);
			if (tempList.numElements == 0)
				app.sessionData.put("tempList" + id, tempList);
			tempList.Add(item);
		}

		public void removeBizObjFromTempBizObjList(IApplication app, IRuleContext context, string id, bool dummy, object key)
		{
			IBizObjList tempList = app.sessionData.get("tempList" + id) as IBizObjList;
			if (tempList != null)
			{
				IBizObjList newList = app.bizObjMgr.createList(tempList.objectType.Name);
				foreach (IBizObj obj in tempList)
					if (((IComparable)obj.KeyValue).CompareTo(key) != 0)
						newList.Add(obj);

				if (newList.numElements > 0)
					app.sessionData.put("tempList" + id, newList);
				else
					app.sessionData.Remove("tempList" + id);
			}
		}

		public void deleteTempBizObjList(IApplication app, IRuleContext context, string id)
		{
			app.sessionData.Remove("tempList" + id);
		}
	}
}
